// pages/detail-songs/index.js
import { rankingStore, playerStore } from '../../store/index'
import { getSongMenuDetail } from "../../server/api_music"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: '',
        ranking: "",
        songInfo: {}
    },

    onLoad: function (options) {
        const type = options.type
        this.setData({ type })
        if (type === "menu") {
            const id = options.id
            getSongMenuDetail(id).then(res => {
             this.setData({ songInfo: res.playlist})
            console.log(res);
            })
        } else if (type === "rank") {
            const ranking = options.ranking
            this.setData({ranking })
            // 获取数据
            rankingStore.onState(ranking, this.getRankingDataHaanlder)
        }
    },
onUnload: function() {
    if(this.data.ranking) {
        rankingStore.offState(this.data.ranking, this.getRankingDataHaanlder)
    }
},
    
getRankingDataHaanlder: function(res) {
    this.setData({songInfo: res})
},

handlSongItemClick: function(event) {
    const index = event.currentTarget.dataset.index
    playerStore.setState( "palyListSongs", this.data.songInfo.tracks)
    playerStore.setState("playListIndex", index)
}

})